package com.itheima.app.manager;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.Notification;
import com.itheima.domain.db.Question;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.SettingVo;
import com.itheima.service.db.BlackListService;
import com.itheima.service.db.NotificationService;
import com.itheima.service.db.QuestionService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;

@Component
public class SettingsManager {

    @Reference
    private QuestionService questionService;
    //保存或者更新通知中问题对象
    public SettingVo saveOrUpdateQuestion(String content) {
        //1.获得用户的id
        Long userId = UserHolder.getId();
        //2.根据用户的id查询用户的问题
        Question question = questionService.findByUid(userId);
        if(question==null){
            //执行保存
            question = new Question();
            question.setUserId(userId);
            question.setStrangerQuestion(content);
            questionService.save(question);
        }else{
            //执行修改
            question.setStrangerQuestion(content);
            questionService.update(question);
        }

        return null;
    }

    @Reference
    private NotificationService notificationService;
    //保存或修改通知数据
    public SettingVo saveOrUpdateNotifications(Notification notification) {
        //1.获得用户的id
        Long userId = UserHolder.getId();
        //2.根据id查询通知对象
        Notification dbNotification = notificationService.findByUserId(userId);
        //3.判断通知对象是否存在 如果存在进行修改 如果不存在进行保存
        if(dbNotification == null ){
            notification.setUserId( userId );//赋值用户id
            notificationService.save(notification);
        }else{
            //赋值
            dbNotification.setGonggaoNotification( notification.getGonggaoNotification() );
            dbNotification.setLikeNotification( notification.getLikeNotification() );
            dbNotification.setPinglunNotification( notification.getPinglunNotification() );
            notificationService.update(dbNotification);
        }
        return null;
    }


    @Reference
    private BlackListService blackListService;
    /**
     * 查询黑名单数据
     * @param page
     * @param pageSize
     * @return
     */
    public PageBeanVo findBlackList(Integer page, Integer pageSize) {
        //分页查询数据
        PageBeanVo pageBeanVo = blackListService.findBlackUserByUserId(UserHolder.getId(), page, pageSize);
        return pageBeanVo;
    }

    //删除黑名单
    public void deleteBlackUser(Long blackUserId) {
        //当前用户id
        Long loginUserId = UserHolder.getId();
        blackListService.deleteBlackUser(loginUserId  ,blackUserId );
    }
}
